package com.platform.modules.sys.service.impl;


import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.common.utils.Query;
import com.platform.modules.sys.dao.SysOrgAptitudeDao;
import com.platform.modules.sys.dao.SysUserAptitudeDao;
import com.platform.modules.sys.entity.SysExamineStatusEntity;
import com.platform.modules.sys.entity.SysOrgAptitudeEntity;
import com.platform.modules.sys.entity.SysRoleEntity;
import com.platform.modules.sys.entity.SysUserAptitudeEntity;
import com.platform.modules.sys.service.SysExamineStatusService;
import com.platform.modules.sys.service.SysOrgAptitudeService;
import com.platform.modules.sys.service.SysUserAptitudeService;
import com.platform.modules.sys.service.SysUserRoleService;

/**
 * @author wangLc
 * 注册机构人员信息      
 * 2019-5-18
 */
@Service("SysUserAptitudeService")
public class SysUserAptitudeServiceImpl extends ServiceImpl<SysUserAptitudeDao, SysUserAptitudeEntity> implements SysUserAptitudeService {

	@Autowired
	private SysExamineStatusService sysExamineStatusService;
	
	

	@Override
	public List<SysUserAptitudeEntity> queryAll(Map<String, Object> params) {
		return baseMapper.selectListPage(params);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] userIds) {
		this.removeByIds(Arrays.asList(userIds));
	}
	
	@Override
	public Page queryPage(Map<String, Object> params) {
		
		if((params.get("aptitudeStatus")+"").equals("2") || (params.get("aptitudeStatus")+"").equals("3")){
			params.put("sidx", "T.AUDIT_TIME");
			params.put("asc", false);
		}else{
			params.put("sidx", "T.CREATE_TIME");
			params.put("asc", false);
		}
		Page<SysUserAptitudeEntity> page = new Query<SysUserAptitudeEntity>(params).getPage();
		List<SysUserAptitudeEntity> list = baseMapper.selectListPage(page, params);
		return page.setRecords(list);
	}

	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(SysUserAptitudeEntity sysUserAptitudeEntity) {
		this.save(sysUserAptitudeEntity);
	}
	
	/**
	 * 审核人员资质
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String auditUserAptitude(String userAptitudeId,String auditOrgNo,String auditStatus,SysUserAptitudeEntity sysUserAptitudeEntity) {
		
		String result = "SUCCESS";
	   	 SysExamineStatusEntity examineStatus = new SysExamineStatusEntity();
	   		Map<String, Object> params = new HashMap<String, Object>();
	   		params.put("auditId", userAptitudeId);
//	   		params.put("auditOrgNo", auditOrgNo);
	   		params.put("auditType", AptitudeEnum.aptitudeType.APTITUDE.getKey());
	   	List<SysExamineStatusEntity> list = sysExamineStatusService.queryAll(params);
	   	if(list != null && list.size()>0){
	   		    examineStatus.setId(list.get(0).getId());
	   		    examineStatus.setAuditTxt("");
	   		    examineStatus.setAuditor(auditOrgNo);
	   		    examineStatus.setAuditOrgNo(auditOrgNo);
	   		    examineStatus.setAuditStatus(auditStatus);
	   		sysExamineStatusService.updateById(examineStatus);
	   	}else{
	   			examineStatus.setAuditTxt("");
	   			examineStatus.setAuditor("");
	   			examineStatus.setAuditOrgNo(auditOrgNo);
	   			examineStatus.setAuditTime(new Date());
	   			examineStatus.setAuditId(userAptitudeId);
	   			examineStatus.setAuditType(AptitudeEnum.aptitudeType.APTITUDE.getKey());
	   			examineStatus.setAuditStatus(auditStatus);
	   	    sysExamineStatusService.save(examineStatus);
	   	}
		sysUserAptitudeEntity.setAptitudeStatus(auditStatus);
		sysUserAptitudeEntity.setAuditTime(new Date());
		this.updateById(sysUserAptitudeEntity);
		return result;
	}
	
}
